package cc.autoapi.pucong.auto.flow.persistence.repository.node;


import cc.autoapi.pucong.auto.flow.persistence.po.node.NodeParamsPo;
import cc.autoapi.pucong.auto.flow.persistence.utils.jdbc.DatabaseUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author pucong
 * @description 节点参数信息 service 实现类
 * @create 2024-09-23
 */
@Service
public class NodeParamsServiceImpl implements NodeParamsService {

    @Override
    public List<NodeParamsPo> list(List<String> nodeInfoIds) {
        if (nodeInfoIds == null || nodeInfoIds.size() < 1) {
            return new ArrayList<>();
        }
        List<Object> params = new ArrayList<>();
        for (String nodeInfoId : nodeInfoIds) {
            params.add(nodeInfoId);
        }
        StringBuffer sqlBf = new StringBuffer();
        sqlBf.append("select * from auto_flow_node_params where 1=1 and node_id in (");
        for (int i = 0; i < nodeInfoIds.size(); i++) {
            sqlBf.append("?,");
        }
        sqlBf.deleteCharAt(sqlBf.length() - 1);
        sqlBf.append(")");
        sqlBf.append(" order by param_code asc ");
        List<NodeParamsPo> apiInfoPos = DatabaseUtils.execQueryListSql(params, sqlBf.toString(), NodeParamsPo.class);
        return apiInfoPos;
    }

    @Override
    public List<NodeParamsPo> list(String nodeId, String paramCode, Integer paramType) {
        List<Object> params = new ArrayList<>();
        StringBuffer sqlBf = new StringBuffer();
        sqlBf.append("select * from auto_flow_node_params where 1=1 ");
        if (StringUtils.isNotBlank(nodeId)) {
            sqlBf.append(" and node_id = ? ");
            params.add(nodeId);
        }
        if (StringUtils.isNotBlank(paramCode)) {
            sqlBf.append(" and param_code = ? ");
            params.add(paramCode);
        }
        if (paramType != null) {
            sqlBf.append(" and param_type = ? ");
            params.add(paramType);
        }
        sqlBf.append(" order by param_code asc ");
        List<NodeParamsPo> apiInfoPos = DatabaseUtils.execQueryListSql(params, sqlBf.toString(), NodeParamsPo.class);
        return apiInfoPos;
    }

    @Override
    public NodeParamsPo getById(String id) {
        List<Object> params = new ArrayList<>();
        StringBuffer sqlBf = new StringBuffer();
        sqlBf.append("select * from auto_flow_node_params where 1=1 and uuid = ? ");
        params.add(id);
        NodeParamsPo apiInstancePo = DatabaseUtils.execQueryOneSql(params, sqlBf.toString(), NodeParamsPo.class);
        return apiInstancePo;
    }

    @Override
    public Boolean save(NodeParamsPo nodeParams) {
        DatabaseUtils.saveAuto("auto_flow_node_params", nodeParams);
        return true;
    }

    @Override
    public Boolean updateById(NodeParamsPo nodeParamsUpdate) {
        DatabaseUtils.updateAutoById("auto_flow_node_params", nodeParamsUpdate);
        return true;
    }

    @Override
    public Boolean removeBatchByIds(List<String> ids) {
        if (ids == null || ids.size() < 1) {
            return false;
        }
        StringBuffer sqlBf = new StringBuffer();
        sqlBf.append("delete from auto_flow_node_params where uuid in (");
        for (int i = 0; i < ids.size(); i++) {
            sqlBf.append("?,");
        }
        sqlBf.deleteCharAt(sqlBf.length() - 1);
        sqlBf.append(")");
        List<Object> params = new ArrayList<>();
        params.addAll(ids);

        DatabaseUtils.execSql(params, sqlBf.toString(), Integer.class);
        return true;
    }
}
